package com.example.student_manager_backend.controller;

import com.example.student_manager_backend.entity.Score;
import com.example.student_manager_backend.status.ScoreStatus;
import com.example.student_manager_backend.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/scores")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @PostMapping
    public ResponseEntity<Score> createScore(@RequestBody Score score) {
        Score createdScore = scoreService.createScore(score);
        return ResponseEntity.ok(createdScore);
    }

    @PutMapping("/{scoreId}")
    public ResponseEntity<Score> updateScore(@PathVariable Long scoreId, @RequestBody Score score) {
        Score updatedScore = scoreService.updateScore(scoreId, score);
        if (updatedScore != null) {
            return ResponseEntity.ok(updatedScore);
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{scoreId}")
    public ResponseEntity<Void> deleteScore(@PathVariable Long scoreId) {
        scoreService.deleteScore(scoreId);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<Page<Score>> findScores(
            Specification<Score> spec,
            @PageableDefault(size = 10) Pageable pageable) {
        Page<Score> scores = scoreService.findScores(spec, pageable);
        return ResponseEntity.ok(scores);
    }

    @GetMapping("/{scoreId}")
    public ResponseEntity<Score> findScoreById(@PathVariable Long scoreId) {
        Score score = scoreService.findScoreById(scoreId);
        if (score != null) {
            return ResponseEntity.ok(score);
        }
        return ResponseEntity.notFound().build();
    }

    @PutMapping("/{scoreId}/review")
    public ResponseEntity<Score> reviewScore(
            @PathVariable Long scoreId,
            @RequestParam ScoreStatus status,
            @RequestParam(required = false) String comment) {
        Score reviewedScore = scoreService.reviewScore(scoreId, status, comment);
        if (reviewedScore != null) {
            return ResponseEntity.ok(reviewedScore);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<Score>> findScoresByStudentId(@PathVariable String studentId) {
        List<Score> scores = scoreService.findScoresByStudentId(studentId);
        return ResponseEntity.ok(scores);
    }

    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<Score>> findScoresByCourseId(@PathVariable String courseId) {
        List<Score> scores = scoreService.findScoresByCourseId(courseId);
        return ResponseEntity.ok(scores);
    }

    @GetMapping("/grade/{grade}")
    public ResponseEntity<List<Score>> findScoresByGrade(@PathVariable String grade) {
        List<Score> scores = scoreService.findScoresByGrade(grade);
        return ResponseEntity.ok(scores);
    }

    @GetMapping("/range")
    public ResponseEntity<List<Score>> findScoresByScoreRange(
            @RequestParam Float minScore,
            @RequestParam Float maxScore) {
        List<Score> scores = scoreService.findScoresByScoreRange(minScore, maxScore);
        return ResponseEntity.ok(scores);
    }
} 